package com.hitalkie.teacher.util;

import android.app.DownloadManager;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import android.net.Uri;
import android.util.DisplayMetrics;
import android.view.WindowManager;
import android.widget.Toast;
import com.hitalkie.teacher.app.TalkieApplication;

import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.Calendar;


/**
 * Date 30/10/2016
 *
 * @author charliema
 */

public class Utils {

    public static String md5(String str) {
        StringBuilder buf = new StringBuilder();
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(str.getBytes());
            byte bytes[] = md5.digest();
            for (byte aByte : bytes) {
                String s = Integer.toHexString(aByte & 0xff);
                if (s.length() == 1) {
                    buf.append("0");
                }
                buf.append(s);
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return buf.toString();
    }


    /**
     * 今天时分、明天时分、月日时分、年月日时分
     *
     * @param timeMilliseconds
     * @return
     */
    public static String getUpdateTime(long timeMilliseconds) {

        Calendar calendarNow = Calendar.getInstance();

        Calendar calendarTarget = Calendar.getInstance();
        calendarTarget.setTimeInMillis(timeMilliseconds);

        if (calendarNow.get(Calendar.YEAR) == calendarTarget.get(Calendar.YEAR)) {
            // 同一年
            calendarTarget.set(Calendar.HOUR, 0);
            calendarTarget.set(Calendar.MINUTE, 0);
            calendarTarget.set(Calendar.SECOND, 0);
            calendarTarget.set(Calendar.MILLISECOND, 0);

            calendarNow.set(Calendar.HOUR, 0);
            calendarNow.set(Calendar.MINUTE, 0);
            calendarNow.set(Calendar.SECOND, 0);
            calendarNow.set(Calendar.MILLISECOND, 0);

            long diff = calendarTarget.getTimeInMillis() - calendarNow.getTimeInMillis();
            long diffDay = diff / (24 * 60 * 60 * 1000);
            if (diffDay == 0) {
                // 今天
                SimpleDateFormat format = new SimpleDateFormat("HH:mm", TalkieApplication.getLocale());
                return "今天 " + format.format(timeMilliseconds);
            } else if (diffDay == 1) {
                // 明天
                SimpleDateFormat format = new SimpleDateFormat("HH:mm", TalkieApplication.getLocale());
                return "明天 " + format.format(timeMilliseconds);
            } else {
                SimpleDateFormat format = new SimpleDateFormat("MM-dd HH:mm", TalkieApplication.getLocale());
                return format.format(timeMilliseconds);
            }
        } else {
            // 不是同一年
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm", TalkieApplication.getLocale());
            return format.format(timeMilliseconds);
        }

    }

    public static String getFormatTime(long timeMilliseconds) {
//        DateFormat format = DateFormat.getDateInstance(DateFormat.FULL, TalkieApplication.getLocale());
//        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd H:m:s");
//        SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd H:m:s");
        SimpleDateFormat format = new SimpleDateFormat("MM-dd HH:mm", TalkieApplication.getLocale());
        return format.format(timeMilliseconds);

    }

    public static int getAppVersionCode(Context context) {
        String pn = context.getPackageName();
        PackageManager pm = context.getPackageManager();
        PackageInfo pi = null;
        try {
            pi = pm.getPackageInfo(pn, 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return pi == null ? 0 : pi.versionCode;
    }

    public static String getAppVersionName(Context context) {
        String pn = context.getPackageName();
        PackageManager pm = context.getPackageManager();
        PackageInfo pi = null;
        try {
            pi = pm.getPackageInfo(pn, 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return pi == null ? "" : pi.versionName;
    }

    public static int dp2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }


    public static int getScreenHeight(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        return outMetrics.heightPixels;
    }

    public static int getScreenWidth(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        return outMetrics.widthPixels;
    }

    public static Long downloadApk(Context c, String url) {
        DownloadManager dm = (DownloadManager) c.getSystemService(Context.DOWNLOAD_SERVICE);
        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
        request.setDestinationInExternalPublicDir("zaiwai", "zaiwai.apk");
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        Long aValue = new Long(0);
        try {
            aValue = dm.enqueue(request);
            Toast.makeText(c, "下载中，请稍后", Toast.LENGTH_LONG).show();
        } catch (Exception e) {
            Toast.makeText(c, "下载失败，请联系管理员", Toast.LENGTH_LONG).show();
        }
        return aValue;
    }

}
